package com.erdos.hyt.mall.service.impl;/**
 * @auth Liurong
 * @date 2017/4/5.
 */

import com.erdos.hyt.base.exception.ExceptionCode;
import com.erdos.hyt.base.exception.business.MallApiException;
import com.erdos.hyt.base.utils.CommonTools;
import com.erdos.hyt.base.utils.CommonUtil;
import com.erdos.hyt.mall.mapper.MallConsigneeAddressMapper;
import com.erdos.hyt.mall.model.MallConsigneeAddress;
import com.erdos.hyt.mall.service.MallConsigneeAddressService;
import com.erdos.hyt.users.mapper.BaseUserMapper;
import com.erdos.hyt.users.model.BaseUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商城地址serviceImpl
 *
 * @author liurong
 * @create 2017/4/5
 **/
@Service("mallConsigneeAddressService")
public class MallConsigneeAddressServiceImpl implements MallConsigneeAddressService {

    @Autowired
    private BaseUserMapper baseUserMapper;

    @Autowired
    private MallConsigneeAddressMapper mallConsigneeAddressMapper;

    @Override
    public MallConsigneeAddress getLatelyAddress(Integer userId) {
       BaseUser baseUser= baseUserMapper.getBaseUserById(userId);
       if(baseUser==null){
           throw new MallApiException(ExceptionCode.USER_UN_REGIST);//获取不到用户
       }
       List<MallConsigneeAddress> list= mallConsigneeAddressMapper.getAddressListByUserId(userId);
        if(list!=null&&list.size()>0){
            return list.get(0);//返回最近一个地址信息
        }
        return null;
    }

    @Override
    public MallConsigneeAddress saveOrUpdAddress(Integer addressId, Integer userId, String consignee, String mobile, String postNum, String address) {
        BaseUser baseUser= baseUserMapper.getBaseUserById(userId);
        if(baseUser==null){
            throw new MallApiException(ExceptionCode.USER_UN_REGIST);//获取不到用户
        }
        boolean isFirst=false;//是否是刚创建
        MallConsigneeAddress mallConsigneeAddress=null;
        if(CommonTools.isNullOrEmpty(addressId)){
            mallConsigneeAddress=new MallConsigneeAddress();
            mallConsigneeAddress.setUserId(userId);
            isFirst=true;
        }else{
           mallConsigneeAddress= mallConsigneeAddressMapper.getMallConsigneeAddressById(addressId);
           if(mallConsigneeAddress==null){
               mallConsigneeAddress=new MallConsigneeAddress();
               mallConsigneeAddress.setUserId(userId);
               isFirst=true;
           }
        }
        mallConsigneeAddress.setAddress(address);
        mallConsigneeAddress.setConsignee(consignee);
        mallConsigneeAddress.setConsigneeMobile(mobile);
        mallConsigneeAddress.setPostNum(postNum);
        mallConsigneeAddress.setCreateUser(userId);
        if(isFirst){
            mallConsigneeAddressMapper.insertMallConsigneeAddress(mallConsigneeAddress);
        }else {
            mallConsigneeAddressMapper.updateMallConsigneeAddress(mallConsigneeAddress);
        }
        return mallConsigneeAddress;
    }
}
